//
//  ConfigManager.m
//  Task Views
//
//  Created by Chris Karr on 4/22/09.
//  Copyright 2009 Audacious Software. All rights reserved.
//

#import "ConfigManager.h"


@implementation ConfigManager

- (void) awakeFromNib
{
	[configs addObserver:self forKeyPath:@"selection" options:0 context:NULL];
	[configs addObserver:self forKeyPath:@"arrangedObjects" options:0 context:NULL];
	
	[self observeValueForKeyPath:@"arrangedObjects" ofObject:nil change:nil context:NULL];

	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(useConfig:) name:CONFIG_NOTE object:nil];
	
	[configs setSelectedObjects:[NSArray array]];
}

- (void) useConfig:(NSNotification *) theNote
{
	[configs setSelectedObjects:[NSArray arrayWithObject:[theNote object]]];
}

- (NSArray *) defaultsKeys
{
	return [NSArray arrayWithObjects:@"hist_attribute", @"hist_ignore_missing", @"hist_background", @"hist_default_color", @"hist_selection_color",
			@"list_columns", @"list_sort_descriptors", @"color_mappings", @"tag_size_ln", @"tag_size_attribute", @"tag_size_multiplier", @"tag_size_adjustment", 
			@"tag_default_color", @"tag_color_attribute", @"cloud_background", @"cal_date_field", @"cal_align", nil];
}

- (IBAction) config:(id) sender
{
	[configPanel orderFront:sender];
}

- (void) observeValueForKeyPath: (NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
	if ([keyPath isEqual:@"selection"])
	{
		NSArray * selected = [configs selectedObjects];
	
		if ([selected count] > 0)
		{
			NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
		
			NSDictionary * config = [selected lastObject];
		
			for (NSString * key in [self defaultsKeys])
				[defaults setValue:[config valueForKey:key] forKey:key];
		
			if ([[config valueForKey:@"hist_open"] boolValue])
				[histWindow orderFront:self];
			else
				[histWindow orderOut:self];

			if ([[config valueForKey:@"list_open"] boolValue])
				[listWindow orderFront:self];
			else
				[listWindow orderOut:self];

			if ([[config valueForKey:@"tags_open"] boolValue])
				[tagsWindow orderFront:self];
			else
				[tagsWindow orderOut:self];

			if ([[config valueForKey:@"cal_open"] boolValue])
				[calendarWindow orderFront:self];
			else
				[calendarWindow orderOut:self];
			
			[histWindow setFrame:[[config valueForKey:@"hist_frame"] rectValue] display:YES];
			[listWindow setFrame:[[config valueForKey:@"list_frame"] rectValue] display:YES];
			[tagsWindow setFrame:[[config valueForKey:@"tags_frame"] rectValue] display:YES];
			[calendarWindow setFrame:[[config valueForKey:@"cal_frame"] rectValue] display:YES];
		
			[filters setSelectedObjects:[config valueForKey:@"filter"]];
		}
	}
	else 
	{
		while ([menu numberOfItems] > 0)
			[menu removeItemAtIndex:0];
		
		for (NSDictionary * config in [configs arrangedObjects])
		{
			NSMenuItem * item = [menu addItemWithTitle:[config valueForKey:@"name"] action:@selector(activate:) keyEquivalent:@""];
			[item setTarget:self];
			[item setTag:[[configs arrangedObjects] indexOfObject:config]];
			[item bind:@"title" toObject:config withKeyPath:@"name" options:[NSDictionary dictionary]];
		}
		
		[menu addItem:[NSMenuItem separatorItem]];
		
		NSMenuItem * configItem = [menu addItemWithTitle:@"All Configurations..." action:@selector(config:) keyEquivalent:@""];
		[configItem setTarget:self];
	}
}

- (IBAction) activate:(id) sender
{
	[configs setSelectionIndex:[sender tag]];
}

- (IBAction) newConfig:(id) sender
{
	NSMutableDictionary * config = [NSMutableDictionary dictionary];
	
	[config setValue:[NSNumber numberWithBool:[histWindow isVisible]] forKey:@"hist_open"];
	[config setValue:[NSNumber numberWithBool:[listWindow isVisible]] forKey:@"list_open"];
	[config setValue:[NSNumber numberWithBool:[tagsWindow isVisible]] forKey:@"tags_open"];
	[config setValue:[NSNumber numberWithBool:[calendarWindow isVisible]] forKey:@"cal_open"];

	[config setValue:[NSValue valueWithRect:[histWindow frame]] forKey:@"hist_frame"];
	[config setValue:[NSValue valueWithRect:[listWindow frame]] forKey:@"list_frame"];
	[config setValue:[NSValue valueWithRect:[tagsWindow frame]] forKey:@"tags_frame"];
	[config setValue:[NSValue valueWithRect:[calendarWindow frame]] forKey:@"cal_frame"];
	
	[config setValue:[filters selectedObjects] forKey:@"filter"];
	
	[listViewManager saveColumns];
	
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	
	for (NSString * key in [self defaultsKeys])
	{
		id value = [defaults valueForKey:key];
		
		if (key != nil)
			[config setValue:value forKey:key];
	}
	
	[config setValue:@"New Configuration" forKey:@"name"];
	
	[configs addObject:config];
	[configs setSelectedObjects:[NSArray arrayWithObject:config]];
}


@end
